<!DOCTYPE html>












  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">






  
  
    
      
    
    
      
    
  <script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script>
  <link href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-flash.min.css" rel="stylesheet">



<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("http://xxxxxxx.xxx"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>





<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">



  <meta name="google-site-verification" content="zrNUWIf25v2J4dC1wliso_DUXEwYoECv4j3h9CE34xM">








  <meta name="baidu-site-verification" content="nuHze5uXZ7">







  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  

  
    
      
    

    
  

  
    
    
    <link href="https://fonts.cat.net/css?family=Lato:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">




<link href="/css/main.css?v=6.4.2" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.4.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.ico?v=6.4.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.4.2">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.2" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.4.2',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="Compose 文件是一个YAML文件，用于定义services、network和volumes。 Compose 文件的默认路径为./docker-compose.yml(后缀为.yml和.yaml都可以)。">
<meta name="keywords" content="docker,docker-compose">
<meta property="og:type" content="article">
<meta property="og:title" content="如何写docker-compose.yml">
<meta property="og:url" content="https://blog.codenote.pro/how-to-write-dockercomposeyml.html">
<meta property="og:site_name" content="九阳的笔记本">
<meta property="og:description" content="Compose 文件是一个YAML文件，用于定义services、network和volumes。 Compose 文件的默认路径为./docker-compose.yml(后缀为.yml和.yaml都可以)。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://codenote.oss-cn-beijing.aliyuncs.com/0_cAGmXPxnXeTE0QFZ.png">
<meta property="og:updated_time" content="2018-10-29T14:22:48.764Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="如何写docker-compose.yml">
<meta name="twitter:description" content="Compose 文件是一个YAML文件，用于定义services、network和volumes。 Compose 文件的默认路径为./docker-compose.yml(后缀为.yml和.yaml都可以)。">
<meta name="twitter:image" content="https://codenote.oss-cn-beijing.aliyuncs.com/0_cAGmXPxnXeTE0QFZ.png">



  <link rel="alternate" href="/atom.xml" title="九阳的笔记本" type="application/atom+xml">




  <link rel="canonical" href="https://blog.codenote.pro/how-to-write-dockercomposeyml.html">



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>如何写docker-compose.yml | 九阳的笔记本</title>
  




<script async src="https://www.googletagmanager.com/gtag/js?id=UA-126970002-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-126970002-1');
</script>



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?07b2f6778635ec267ee2b4753e118da9";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
     
    
    
    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">九阳的笔记本</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-hexo">
    <a href="/categories/Hexo" rel="section">
      <i class="menu-item-icon fa fa-fw fa-header"></i> <br>Hexo</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-linux">
    <a href="/categories/Linux" rel="section">
      <i class="menu-item-icon fa fa-fw fa-linux"></i> <br>Linux</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-docker">
    <a href="/categories/docker" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>docker</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-learn">
    <a href="/categories/learn" rel="section">
      <i class="menu-item-icon fa fa-fw fa-leanpub"></i> <br>学习</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-top">
    <a href="/top/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-signal"></i> <br>阅读排行</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-sitemap">
    <a href="/sitemap.xml" rel="section">
      <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>站点地图</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.codenote.pro/how-to-write-dockercomposeyml.html">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="yszar">
      <meta itemprop="description" content="学无止境！">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="九阳的笔记本">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">如何写docker-compose.yml
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-10-07 22:44:33" itemprop="dateCreated datePublished" datetime="2018-10-07T22:44:33+08:00">2018-10-07</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-10-29 22:22:48" itemprop="dateModified" datetime="2018-10-29T22:22:48+08:00">2018-10-29</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/docker/" itemprop="url" rel="index"><span itemprop="name">docker</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/how-to-write-dockercomposeyml.html" class="leancloud_visitors" data-flag-title="如何写docker-compose.yml">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p class="description">Compose 文件是一个YAML文件，用于定义services、network和volumes。 Compose 文件的默认路径为./docker-compose.yml(后缀为.yml和.yaml都可以)。</p>

<p><img src="https://codenote.oss-cn-beijing.aliyuncs.com/0_cAGmXPxnXeTE0QFZ.png" alt="" style="width:100%"></p>
<a id="more"></a>
<p>Compose 文件是一个YAML文件，用于定义<code>services</code>、<code>netword</code>和<code>volumes</code>。 Compose 文件的默认路径为<code>./docker-compose.yml</code>(后缀为.yml和.yaml都可以)。</p>
<p>一个<code>service</code>配置将会应用到容器的启动中，很像将命令行参数传递给<code>docker run</code>。 同样，network和volume定义类似于<code>docker network create</code>和<code>docker volume create</code>。 与Docker运行一样，默认情况下尊重Dockerfile中指定的选项（例如<code>CMD</code>，<code>EXPOSE</code>，<code>VOLUME</code>，<code>ENV</code>） - 您不需要在<code>docker-compose.yml</code>中再次指定它们。</p>
<p>您可以在配置中使用具有类似Bash的<code>${VARIABLE}</code>语法使用环境变量 - 有关详细信息，请参阅<a href="#toc_61">Variable substitution</a>。</p>
<h3 id="Service-配置参考"><a href="#Service-配置参考" class="headerlink" title="Service 配置参考"></a>Service 配置参考</h3><blockquote class="question"><code>注意</code>：有多个版本的Compose文件格式 - 版本1（旧版格式，不支持卷或网络）和版本2以及2.1（最新的）。 有关更多信息，请参阅版[Versioning]部分。<br></blockquote>

<p>此部分包含服务定义支持的所有配置选项的列表。</p>
<h4 id="build"><a href="#build" class="headerlink" title="build"></a>build</h4><p>在构建时应用的配置选项。</p>
<p><code>build</code>可以指定为包含构建上下文路径，也可以根据上下文选择 dockerfile 和 args 指定的路径对象。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></div><div class="code"><pre><div class="line"><span class="attr">    build:</span> <span class="string">./dir</span></div><div class="line">    </div><div class="line"><span class="attr">    build:</span></div><div class="line"><span class="attr">      context:</span> <span class="string">./dir</span></div><div class="line"><span class="attr">      dockerfile:</span> <span class="string">Dockerfile-alternate</span></div><div class="line"><span class="attr">      args:</span></div><div class="line"><span class="attr">        buildno:</span> <span class="number">1</span></div><div class="line"><span class="string">```</span>  </div><div class="line"></div><div class="line"><span class="string">如果指定`image`字段来`build`，则`Compose`使用`image`中指定的`webapp`和`tag`（可选）命名构建的`image`：</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">    build:</span> <span class="string">./dir</span></div><div class="line"><span class="attr">    image:</span> <span class="attr">webapp:tag</span></div></pre></div></div></figure>
<p>这里从<code>./dir</code>构建了一个镜像名为<code>webapp</code>，并打上标记为<code>tag</code>的镜像。</p>
<div class="note warning"><p> <code>注意</code>：在版本1文件格式中，build有两种不同的方法：<br><br> 1.  只允许字符串形式（build：.） - 而不是对象形式。<br> 2.  不允许与<code>image</code>一起<code>build</code>。 尝试这样做会导致错误。</p></div>

<h5 id="context"><a href="#context" class="headerlink" title="context"></a>context</h5><div class="note info"><p>版本2及以上使用，版本1只使用build。</p></div>

<p>它的值可以是<code>Dockerfile</code>的路径，或者是指向<code>git</code>仓库的url。</p>
<p>当提供的值是相对路径时，它被解释Compose文件位置的相对路径。 目录里的信息会被当做构建内容 发送到Docker守护进程。</p>
<p>Compose将使用生成的名字<code>build</code>和<code>tag</code>它，然后使用该<code>image</code>。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></div><div class="code"><pre><div class="line"><span class="attr">    build:</span></div><div class="line"><span class="attr">      context:</span> <span class="string">./dir</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">##### dockerfile</span></div><div class="line"></div><div class="line"><span class="string">备用Dockerfile。</span></div><div class="line"></div><div class="line"><span class="string">撰写将使用一个备用文件来构建。</span> <span class="string">还必须指定构建路径。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">    build:</span></div><div class="line"><span class="attr">      context:</span> <span class="string">.</span></div><div class="line"><span class="attr">      dockerfile:</span> <span class="string">Dockerfile-alternate</span></div></pre></div></div></figure>
<blockquote class="question"><br> <code>注意</code>：在版本1文件格式中，dockerfile有两种不同的方法：<br><br> 1.  它与build同级出现，而不是子选项：<br><br> <figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></div><div class="code"><pre><div class="line"><span class="attr">       build:</span> <span class="string">.</span></div><div class="line"><span class="attr">       dockerfile:</span> <span class="string">Dockerfile-alternate</span></div><div class="line"><span class="string">```</span>     </div><div class="line"> </div><div class="line"> <span class="number">1.</span>  <span class="string">不允许与image一起使用dockerfile。</span> <span class="string">尝试这样做会导致错误。&lt;/blockquote&gt;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">##### args</span></div><div class="line"></div><div class="line"><span class="string">&lt;div</span> <span class="string">class="note</span> <span class="string">info"&gt;&lt;p&gt;</span> <span class="string">支持版本2及以上</span></div><div class="line"><span class="string">&lt;/p&gt;&lt;/div&gt;</span></div><div class="line"></div><div class="line"><span class="string">添加构建参数，它们是只有在构建过程中才可访问的环境变量。</span></div><div class="line"></div><div class="line"><span class="string">首先，在Dockerfile中指定参数：</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line">    <span class="string">ARG</span> <span class="string">buildno</span></div><div class="line">    <span class="string">ARG</span> <span class="string">password</span></div><div class="line">    </div><div class="line">    <span class="string">RUN</span> <span class="string">echo</span> <span class="string">"Build number: $buildno"</span></div><div class="line">    <span class="string">RUN</span> <span class="string">script-requiring-password.sh</span> <span class="string">"$password"</span></div></pre></div></div></figure><br><br><br>然后，指定<code>build</code>字段下的参数<code>args</code>。您可以mapping或list：<br><br><figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></div><div class="code"><pre><div class="line"><span class="attr">build:</span></div><div class="line"><span class="attr">  context:</span> <span class="string">.</span></div><div class="line"><span class="attr">  args:</span></div><div class="line"><span class="attr">    buildno:</span> <span class="number">1</span></div><div class="line"><span class="attr">    password:</span> <span class="string">secret</span></div><div class="line"></div><div class="line"><span class="attr">build:</span></div><div class="line"><span class="attr">  context:</span> <span class="string">.</span></div><div class="line"><span class="attr">  args:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">buildno=1</span></div><div class="line"><span class="bullet">    -</span> <span class="string">password=secret</span></div></pre></div></div></figure><br><br><br>指定build参数时，可以忽略该值，在这种情况下，构建时其值为运行Compose的环境中的值。<br><br><figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></div><div class="code"><pre><div class="line"><span class="attr">    args:</span></div><div class="line"><span class="bullet">      -</span> <span class="string">buildno</span></div><div class="line"><span class="bullet">      -</span> <span class="string">password</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="string">&lt;blockquote</span> <span class="string">class="question"&gt;`注意`：YAML布尔值（true，false，yes，no，on，off）必须用引号引起来，以便解析器将其解释为字符串。</span></div><div class="line"><span class="string">&lt;/blockquote&gt;</span></div><div class="line"></div><div class="line"><span class="comment">#### cap\_add, cap\_drop</span></div><div class="line"></div><div class="line"><span class="string">添加或删除容器功能。</span> <span class="string">有关完整列表，请参阅man</span> <span class="number">7</span><span class="string">功能。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">    cap_add:</span></div><div class="line"><span class="bullet">      -</span> <span class="string">ALL</span></div><div class="line">    </div><div class="line"><span class="attr">    cap_drop:</span></div><div class="line"><span class="bullet">      -</span> <span class="string">NET_ADMIN</span></div><div class="line"><span class="bullet">      -</span> <span class="string">SYS_ADMIN</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### command</span></div><div class="line"></div><div class="line"><span class="string">覆盖默认命令</span></div><div class="line"></div><div class="line"><span class="string">```bash</span></div><div class="line"><span class="attr">    command:</span> <span class="string">bundle</span> <span class="string">exec</span> <span class="string">thin</span> <span class="bullet">-p</span> <span class="number">3000</span></div></pre></div></div></figure><br><br><br>该命令也可以是一个list，在方式类似于dockerfile：<br><br><figure class="highlight bash"><div class="figcode"><div class="gutter"><pre><div class="line">1</div></pre></div><div class="code"><pre><div class="line"><span class="built_in">command</span>: [bundle, <span class="built_in">exec</span>, thin, -p, 3000]</div></pre></div></div></figure><br><br><br>#### cgroup_parent<br><br>为容器指定可选的父cgroup。<br><br><figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></div><div class="code"><pre><div class="line"><span class="attr">    cgroup_parent:</span> <span class="string">m-executor-abcd</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### container_name</span></div><div class="line"></div><div class="line"><span class="string">指定自定义容器名称，而不是生成的默认名称。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">    container_name:</span> <span class="string">my-web-container</span></div></pre></div></div></figure><br><br><br>由于Docker容器名称必须是唯一的，因此如果您指定了自定义名称，则无法将服务扩展到1个容器之外。 尝试这样做会导致错误。<br><br>#### devices<br><br>设备映射列表。 使用与<code>--device</code> docker client create选项相同的格式。<br><br><figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></div><div class="code"><pre><div class="line"><span class="attr">devices:</span></div><div class="line"><span class="bullet">  -</span> <span class="string">"/dev/ttyUSB0:/dev/ttyUSB0"</span></div></pre></div></div></figure><br><br><br>#### depends_on<br><br>Express之间的依赖关系，有两个效果：<br><br><em>   <code>docker-compose up</code> 将按照依赖顺序启动服务。 在下面的示例中，db和redis将在web之前启动。
</em>   <code>docker-compose up SERVICE</code> 将自动包含SERVICE的依赖关系。 在以下示例中，docker-compose up web也将创建并启动db和redis。<br><br>列子：<br><br><figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">  web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">.</span></div><div class="line"><span class="attr">    depends_on:</span></div><div class="line"><span class="bullet">      -</span> <span class="string">db</span></div><div class="line"><span class="bullet">      -</span> <span class="string">redis</span></div><div class="line"><span class="attr">  redis:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">redis</span></div><div class="line"><span class="attr">  db:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">postgres</span></div></pre></div></div></figure><br><br><br><blockquote class="question"> <code>注意</code>：在启动web之前，depends_on不会等待db和redis“就绪”，直到它们被启动。 如果您需要等待服务准备就绪，请参阅控制<a href="https://docs.docker.com/compose/startup-order/" rel="external nofollow noopener noreferrer" target="_blank">启动顺序</a>了解有关此问题的更多信息以及解决问题的策略。<br></blockquote>

<h4 id="dns"><a href="#dns" class="headerlink" title="dns"></a>dns</h4><p>自定义DNS服务器。可以是单个值或列表。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></div><div class="code"><pre><div class="line"><span class="attr">    dns:</span> <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span></div><div class="line"><span class="attr">    dns:</span></div><div class="line"><span class="bullet">      -</span> <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span></div><div class="line"><span class="bullet">      -</span> <span class="number">9.9</span><span class="number">.9</span><span class="number">.9</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### dns_search</span></div><div class="line"></div><div class="line"><span class="string">自定义DNS搜索域。可以是单个值或列表。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">    dns_search:</span> <span class="string">example.com</span></div><div class="line"><span class="attr">    dns_search:</span></div><div class="line"><span class="bullet">      -</span> <span class="string">dc1.example.com</span></div><div class="line"><span class="bullet">      -</span> <span class="string">dc2.example.com</span></div></pre></div></div></figure>
<h4 id="tmpfs"><a href="#tmpfs" class="headerlink" title="tmpfs"></a>tmpfs</h4><p>在容器中安装临时文件系统。可以是单个值或列表。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></div><div class="code"><pre><div class="line"><span class="attr">    tmpfs:</span> <span class="string">/run</span></div><div class="line"><span class="attr">    tmpfs:</span></div><div class="line"><span class="bullet">      -</span> <span class="string">/run</span></div><div class="line"><span class="bullet">      -</span> <span class="string">/tmp</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### entrypoint</span></div><div class="line"></div><div class="line"><span class="string">覆盖默认的`entrypoint`。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">    entrypoint:</span> <span class="string">/code/entrypoint.sh</span></div></pre></div></div></figure>
<p>entrypoint也可以是一个列表，存在方式类似于dockerfile：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></div><div class="code"><pre><div class="line"><span class="attr">entrypoint:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">php</span></div><div class="line"><span class="bullet">    -</span> <span class="bullet">-d</span></div><div class="line"><span class="bullet">    -</span> <span class="string">zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so</span></div><div class="line"><span class="bullet">    -</span> <span class="bullet">-d</span></div><div class="line"><span class="bullet">    -</span> <span class="string">memory_limit=-1</span></div><div class="line"><span class="bullet">    -</span> <span class="string">vendor/bin/phpunit</span></div></pre></div></div></figure>
<h4 id="env-file"><a href="#env-file" class="headerlink" title="env_file"></a>env_file</h4><p>从文件添加环境变量。可以是单个值或列表。</p>
<p>如果已使用<code>docker-compose -f FILE</code>指定了一个Compose文件，则<code>env_file</code>中的路径相对于该文件所在的目录。</p>
<p>在环境中指定的环境变量会覆盖这些值。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></div><div class="code"><pre><div class="line"><span class="attr">env_file:</span> <span class="string">.env</span></div><div class="line"></div><div class="line"><span class="attr">env_file:</span></div><div class="line"><span class="bullet">  -</span> <span class="string">./common.env</span></div><div class="line"><span class="bullet">  -</span> <span class="string">./apps/web.env</span></div><div class="line"><span class="bullet">  -</span> <span class="string">/opt/secrets.env</span></div></pre></div></div></figure>
<p>Compose期望env文件中的每一行都处于<code>VAR = VAL</code>格式。 以＃开头的行（即注释）将被忽略，空行也是如此。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></div><div class="code"><pre><div class="line"><span class="comment"># Set Rails/Rack environment</span></div><div class="line"><span class="string">RACK_ENV=development</span></div></pre></div></div></figure>
<blockquote class="question"> <code>注意</code>：如果您的service指定了build选项，则在build过程中将不会自动显示环境文件中定义的变量。 使用build的args子选项来定义构建时环境变量。<br></blockquote>

<h4 id="environment"><a href="#environment" class="headerlink" title="environment"></a>environment</h4><p>添加环境变量。您可以使用数组或字典。 任意布尔值:true，false，yes no，需要用引号括起来，以确保它们不会被YML解析器转换为True或False。</p>
<p>只有一个键的环境变量被解析为它在Compose正在运行的机器上的值，这对于secret或host-specific values是有帮助的。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></div><div class="code"><pre><div class="line"><span class="attr">environment:</span></div><div class="line"><span class="attr">    RACK_ENV:</span> <span class="string">development</span></div><div class="line"><span class="attr">    SHOW:</span> <span class="string">'true'</span></div><div class="line"><span class="attr">    SESSION_SECRET:</span></div><div class="line"></div><div class="line"><span class="attr">environment:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">RACK_ENV=development</span></div><div class="line"><span class="bullet">    -</span> <span class="string">SHOW=true</span></div><div class="line"><span class="bullet">    -</span> <span class="string">SESSION_SECRET</span></div></pre></div></div></figure>
<blockquote class="question"> <code>注意</code>：如果您的服务指定了build选项，则在build期间将不会自动显示在环境中定义的变量。 使用build的args子选项来定义构建时环境变量。<br></blockquote>

<h4 id="expose"><a href="#expose" class="headerlink" title="expose"></a>expose</h4><p>公开端口，而不将其发布到主机 - 它们只能访问链接服务。只能指定内部端口。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">expose:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"3000"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"8000"</span></div></pre></div></div></figure>
<h4 id="extends"><a href="#extends" class="headerlink" title="extends"></a>extends</h4><p>在当前文件或另一个文件中扩展另一个服务，可选地覆盖配置。</p>
<p>您可以将<code>extends</code>与其他配置字段一起使用。 <code>extends</code>值必须是使用必需的<code>server</code>和可选<code>file</code>字段定义的字典。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">extends:</span></div><div class="line"><span class="attr">    file:</span> <span class="string">common.yml</span></div><div class="line"><span class="attr">    service:</span> <span class="string">webapp</span></div></pre></div></div></figure>
<p><code>server</code>正在扩展的服务的名称，例如<code>web</code>或数据库。 该文件是定义该服务的Compose配置文件的位置。</p>
<p>如果省略<code>file</code> Compose在当前文件中查找服务配置。 <code>file</code>值可以是绝对路径或相对路径。 如果指定相对路径，则Compose将其视为相对于当前文件的位置。</p>
<p>您可以扩展本身扩展其他服务。 您可以无限期地延长。 Compose不支持循环引用，如果遇到一个，docker-compose会返回一个错误。</p>
<p>有关扩展的更多信息，请参阅<a href="https://docs.docker.com/compose/extends/#extending-services" rel="external nofollow noopener noreferrer" target="_blank">扩展文档</a>。</p>
<h4 id="external-links"><a href="#external-links" class="headerlink" title="external_links"></a>external_links</h4><p>链接到在<code>docker-compose.yml</code>外部启动的容器，甚至在Compose之外，特别是对于提供共享或公共服务的容器。 <code>external_links</code>在指定容器名称和链接别名（<code>CONTAINER：ALIAS</code>）时遵循类似于<code>links</code>的语义。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></div><div class="code"><pre><div class="line"><span class="attr">external_links:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">redis_1</span></div><div class="line"><span class="attr">    - project_db_1:</span><span class="string">mysql</span></div><div class="line"><span class="attr">    - project_db_1:</span><span class="string">postgresql</span></div></pre></div></div></figure>
<blockquote class="question"> <code>注意</code>：如果您使用的是版本2文件格式，则外部创建的容器必须连接到至少一个与链接到它们的服务相同的网络。<br></blockquote>

<h4 id="extra-hosts"><a href="#extra-hosts" class="headerlink" title="extra_hosts"></a>extra_hosts</h4><p>添加主机名映射。使用与docker client <code>--add-host</code>参数相同的值。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">extra_hosts:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"somehost:162.242.195.82"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"otherhost:50.31.209.229"</span></div></pre></div></div></figure>
<p>具有ip地址和主机名的条目将在此服务的容器中的<code>/etc/hosts</code>中创建，例如：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></div><div class="code"><pre><div class="line"><span class="number">162.242</span><span class="number">.195</span><span class="number">.82</span>  <span class="string">somehost</span></div><div class="line"><span class="number">50.31</span><span class="number">.209</span><span class="number">.229</span>   <span class="string">otherhost</span></div></pre></div></div></figure>
<h4 id="group-add"><a href="#group-add" class="headerlink" title="group_add"></a>group_add</h4><p>指定容器中的用户将成为其成员的其他组（按名称或号码）。 组必须存在于要添加的容器和主机系统中。 这是有用的一个例子是当多个容器（作为不同的用户运行）需要在主机系统上读取或写入相同的文件。 该文件可以由所有容器共享的组拥有，并在<code>group_add</code>中指定。 有关更多详细信息，请参阅<a href="https://docs.docker.com/engine/reference/run/#additional-groups" rel="external nofollow noopener noreferrer" target="_blank">Docker文档</a>。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">alpine</span></div><div class="line"><span class="attr">    group_add:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">mail</span></div></pre></div></div></figure>
<p>在创建的容器中运行<code>id</code>将显示用户属于<code>mail</code>组，如果未使用<code>group_add</code>，则不会是这种情况。</p>
<h4 id="image"><a href="#image" class="headerlink" title="image"></a>image</h4><p>指定要从中启动容器的映像。可以是存储库/标记或部分图像ID。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></div><div class="code"><pre><div class="line"><span class="attr">image:</span> <span class="string">redis</span></div><div class="line"><span class="attr">image:</span> <span class="attr">ubuntu:14.04</span></div><div class="line"><span class="attr">image:</span> <span class="string">tutum/influxdb</span></div><div class="line"><span class="attr">image:</span> <span class="string">example-registry.com:4000/postgresql</span></div><div class="line"><span class="attr">image:</span> <span class="string">a4bc65fd</span></div></pre></div></div></figure>
<p>如果image不存在，Compose会尝试拉取它，除非你也指定了build，在这种情况下，它使用指定的选项构建它，并用指定的标签标记它。</p>
<blockquote class="question"> <code>注意</code>：在版本1文件格式中，不允许与image一起使用build。 尝试这样做会导致错误。<br></blockquote>

<h4 id="isolation"><a href="#isolation" class="headerlink" title="isolation"></a>isolation</h4><blockquote class="question"> Added in version 2.1 file format. 指定容器的隔离技术。在Linux上，唯一支持的值为<code>default</code>。 在Windows上，可接受的值为<code>default</code>，<code>process</code>和<code>hyperv</code>。 有关详细信息，请参阅<a href="https://docs.docker.com/engine/reference/commandline/run/#specify-isolation-technology-for-container---isolation" rel="external nofollow noopener noreferrer" target="_blank">Docker Engine文档</a>。</blockquote>


<h4 id="labels"><a href="#labels" class="headerlink" title="labels"></a>labels</h4><p>使用<a href="https://docs.docker.com/engine/userguide/labels-custom-metadata/" rel="external nofollow noopener noreferrer" target="_blank">Docker labels</a>向容器添加元数据。 您可以使用list或mapping。</p>
<p>建议您使用reverse-DNS符号来防止您的标签与其他软件使用的标签冲突。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></div><div class="code"><pre><div class="line"><span class="attr">labels:</span></div><div class="line">    <span class="string">com.example.description:</span> <span class="string">"Accounting webapp"</span></div><div class="line">    <span class="string">com.example.department:</span> <span class="string">"Finance"</span></div><div class="line">    <span class="string">com.example.label-with-empty-value:</span> <span class="string">""</span></div><div class="line"></div><div class="line"><span class="attr">labels:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.description=Accounting webapp"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.department=Finance"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.label-with-empty-value"</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### links</span></div><div class="line"></div><div class="line"><span class="string">链接到另一个服务中的容器。请指定服务名称和链接别名（`SERVICE：ALIAS`），或仅指定服务名称。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">web:</span></div><div class="line"><span class="attr">    links:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">db</span></div><div class="line"><span class="attr">    - db:</span><span class="string">database</span></div><div class="line"><span class="bullet">    -</span> <span class="string">redis</span></div></pre></div></div></figure>
<p>如果未指定别名，则可以在与别名相同的主机名或服务名称处访问链接服务的容器。</p>
<p>链接也以与<a href="https://docs.docker.com/compose/compose-file/#dependson" rel="external nofollow noopener noreferrer" target="_blank">depends_on</a>相同的方式表示服务之间的依赖关系，因此它们确定服务启动的顺序。</p>
<blockquote>
<p><code>注意</code>：如果您定义链接和<a href="https://docs.docker.com/compose/compose-file/#networks" rel="external nofollow noopener noreferrer" target="_blank">networks</a>，则具有它们之间的链接的服务必须共享至少一个公共网络以便进行</p>
</blockquote>
<h4 id="logging"><a href="#logging" class="headerlink" title="logging"></a>logging</h4><blockquote class="question"> Version 2 file format and up. In version 1, use log_driver and log_opt.<br></blockquote>

<p>记录服务的配置。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></div><div class="code"><pre><div class="line"><span class="attr">logging:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">syslog</span></div><div class="line"><span class="attr">    options:</span></div><div class="line"><span class="attr">    syslog-address:</span> <span class="string">"tcp://192.168.0.42:123"</span></div></pre></div></div></figure>
<p><code>driver</code>名称指定了服务容器的日志驱动程序，，如docker运行的<code>--log-driver</code>选项（<a href="https://docs.docker.com/engine/reference/logging/overview/" rel="external nofollow noopener noreferrer" target="_blank">documented here</a>）。</p>
<p>默认值为json-file。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">driver:</span> <span class="string">"json-file"</span></div><div class="line"><span class="attr">driver:</span> <span class="string">"syslog"</span></div><div class="line"><span class="attr">driver:</span> <span class="string">"none"</span></div></pre></div></div></figure>
<blockquote class="question"> <code>注意</code>：只有<code>json-file</code>和<code>journald</code>驱动程序使日志可以直接从<code>docker-compose up</code>和<code>docker-compose logs</code>获取。 使用任何其他驱动程序将不会打印任何日志。<br></blockquote>

<p>使用<code>options</code>键为记录驱动程序指定日志记录选项，例如<code>docker run</code>的<code>--log-opt</code>选项。</p>
<p>日志记录选项是<code>key-value</code>对。 <code>syslog</code>选项的示例：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">driver:</span> <span class="string">"syslog"</span></div><div class="line"><span class="attr">options:</span></div><div class="line"><span class="attr">    syslog-address:</span> <span class="string">"tcp://192.168.0.42:123"</span></div></pre></div></div></figure>
<h4 id="log-driver"><a href="#log-driver" class="headerlink" title="log_driver"></a>log_driver</h4><blockquote class="question"> Version 1 file format only. In version 2, use logging.<br></blockquote>

<p>指定日志驱动程序。默认值为<code>json-file</code>。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div></pre></div><div class="code"><pre><div class="line"><span class="attr">log_driver:</span> <span class="string">syslog</span></div></pre></div></div></figure>
<h4 id="log-opt"><a href="#log-opt" class="headerlink" title="log_opt"></a>log_opt</h4><blockquote class="question"> Version 1 file format only. In version 2, use logging.<br></blockquote>

<p>将记录选项指定为<code>key-value</code>对。 <code>syslog</code>选项的示例：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></div><div class="code"><pre><div class="line"><span class="attr">log_opt:</span></div><div class="line"><span class="attr">    syslog-address:</span> <span class="string">"tcp://192.168.0.42:123"</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### net</span></div><div class="line"></div><div class="line"><span class="string">&lt;blockquote</span> <span class="string">class="question"&gt;</span> <span class="string">Version</span> <span class="number">1</span> <span class="string">file</span> <span class="string">format</span> <span class="string">only.</span> <span class="string">In</span> <span class="string">version</span> <span class="number">2</span><span class="string">,</span> <span class="string">use</span> <span class="string">network_mode.</span></div><div class="line"><span class="string">&lt;/blockquote&gt;</span></div><div class="line"></div><div class="line"><span class="string">网络模式。</span> <span class="string">使用与docker</span> <span class="string">client</span> <span class="string">`--net`参数相同的值。</span> <span class="string">`container：...`形式可以接受服务名称，而不是容器名称或ID。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">net:</span> <span class="string">"bridge"</span></div><div class="line"><span class="attr">net:</span> <span class="string">"host"</span></div><div class="line"><span class="attr">net:</span> <span class="string">"none"</span></div><div class="line"><span class="attr">net:</span> <span class="string">"container:[service name or container name/id]"</span></div></pre></div></div></figure>
<h4 id="network-mode"><a href="#network-mode" class="headerlink" title="network_mode"></a>network_mode</h4><blockquote class="question"> Version 2 file format and up. In version 1, use net.<br></blockquote>

<p>网络模式。 使用与docker client <code>--net</code>参数相同的值，以及特殊表单 <code>service：[service name]</code>。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></div><div class="code"><pre><div class="line"><span class="attr">network_mode:</span> <span class="string">"bridge"</span></div><div class="line"><span class="attr">network_mode:</span> <span class="string">"host"</span></div><div class="line"><span class="attr">network_mode:</span> <span class="string">"none"</span></div><div class="line"><span class="attr">network_mode:</span> <span class="string">"service:[service name]"</span></div><div class="line"><span class="attr">network_mode:</span> <span class="string">"container:[container name/id]"</span></div></pre></div></div></figure>
<h4 id="networks"><a href="#networks" class="headerlink" title="networks"></a>networks</h4><blockquote class="question"> Version 2 file format and up. In version 1, use net.<br></blockquote>

<p>要加入的网络, 引用<a href="https://docs.docker.com/compose/compose-file/#network-configuration-reference" rel="external nofollow noopener noreferrer" target="_blank">top-level networks key</a> 下的条目。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></div><div class="code"><pre><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    some-service:</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">some-network</span></div><div class="line"><span class="bullet">        -</span> <span class="string">other-network</span></div></pre></div></div></figure>
<h5 id="aliases"><a href="#aliases" class="headerlink" title="aliases"></a>aliases</h5><p>网络上此服务的别名（备用主机名）。同一网络上的其他容器可以使用服务名称或此别名连接到服务的容器之一。</p>
<p>由于<code>aliases</code>是网络范围的，所以相同的服务可以在不同的网络上具有不同的别名。</p>
<blockquote class="question"> 注意：网络范围的别名可以由多个容器共享，甚至可以由多个服务共享。 如果是，那么该名称将解析为哪个容器不能保证。<br></blockquote>

<p>一般格式如下所示。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></div><div class="code"><pre><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    some-service:</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="attr">        some-network:</span></div><div class="line"><span class="attr">        aliases:</span></div><div class="line"><span class="bullet">            -</span> <span class="string">alias1</span></div><div class="line"><span class="bullet">            -</span> <span class="string">alias3</span></div><div class="line"><span class="attr">        other-network:</span></div><div class="line"><span class="attr">        aliases:</span></div><div class="line"><span class="bullet">            -</span> <span class="string">alias2</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="string">在下面的示例中，提供了三个服务（`web`，`worker`和`db`），以及两个网络（`new`和`legacy`）。</span> <span class="string">`db`服务在`new`网络上的主机名`db`或`database`，`legacy`网络中的`db`或`mysql`是可达的。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">./web</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">new</span></div><div class="line"></div><div class="line"><span class="attr">    worker:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">./worker</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">legacy</span></div><div class="line"></div><div class="line"><span class="attr">    db:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">mysql</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="attr">        new:</span></div><div class="line"><span class="attr">        aliases:</span></div><div class="line"><span class="bullet">            -</span> <span class="string">database</span></div><div class="line"><span class="attr">        legacy:</span></div><div class="line"><span class="attr">        aliases:</span></div><div class="line"><span class="bullet">            -</span> <span class="string">mysql</span></div><div class="line"></div><div class="line"><span class="attr">networks:</span></div><div class="line"><span class="attr">    new:</span></div><div class="line"><span class="attr">    legacy:</span></div></pre></div></div></figure>
<h5 id="ipv4-address-ipv6-address"><a href="#ipv4-address-ipv6-address" class="headerlink" title="ipv4_address, ipv6_address"></a>ipv4_address, ipv6_address</h5><p>在加入网络时为此服务的容器指定静态IP地址。</p>
<p>顶级网络部分中的相应网络配置必须具有包含每个静态地址的子网和网关配置的ipam块。 如果需要IPv6寻址，则com.docker.network.enable_ipv6驱动程序选项必须设置为true。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    app:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">busybox</span></div><div class="line"><span class="attr">    command:</span> <span class="string">ifconfig</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="attr">        app_net:</span></div><div class="line"><span class="attr">        ipv4_address:</span> <span class="number">172.16</span><span class="number">.238</span><span class="number">.10</span></div><div class="line"><span class="attr">        ipv6_address:</span> <span class="number">2001</span><span class="string">:3984:3989::10</span></div><div class="line"></div><div class="line"><span class="attr">networks:</span></div><div class="line"><span class="attr">    app_net:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">bridge</span></div><div class="line"><span class="attr">    driver_opts:</span></div><div class="line">        <span class="string">com.docker.network.enable_ipv6:</span> <span class="string">"true"</span></div><div class="line"><span class="attr">    ipam:</span></div><div class="line"><span class="attr">        driver:</span> <span class="string">default</span></div><div class="line"><span class="attr">        config:</span></div><div class="line"><span class="attr">        - subnet:</span> <span class="number">172.16</span><span class="number">.238</span><span class="number">.0</span><span class="string">/24</span></div><div class="line"><span class="attr">        gateway:</span> <span class="number">172.16</span><span class="number">.238</span><span class="number">.1</span></div><div class="line"><span class="attr">        - subnet:</span> <span class="number">2001</span><span class="string">:3984:3989::/64</span></div><div class="line"><span class="attr">        gateway:</span> <span class="number">2001</span><span class="string">:3984:3989::1</span></div></pre></div></div></figure>
<h5 id="link-local-ips"><a href="#link-local-ips" class="headerlink" title="link_local_ips"></a>link_local_ips</h5><blockquote class="question"> Added in version 2.1 file format.<br></blockquote>

<p>指定link-local IPs。 link-local IPs是属于众所周知的子网并且完全由运营商管理的特殊IP，通常取决于它们被部署的架构。 因此，它们不由docker（IPAM driver）管理。</p>
<p>用法示例：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2.1'</span></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    app:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">busybox</span></div><div class="line"><span class="attr">    command:</span> <span class="string">top</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="attr">        app_net:</span></div><div class="line"><span class="attr">        link_local_ips:</span></div><div class="line"><span class="bullet">            -</span> <span class="number">57.123</span><span class="number">.22</span><span class="number">.11</span></div><div class="line"><span class="bullet">            -</span> <span class="number">57.123</span><span class="number">.22</span><span class="number">.13</span></div><div class="line"><span class="attr">networks:</span></div><div class="line"><span class="attr">    app_net:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">bridge</span></div></pre></div></div></figure>
<h4 id="pid"><a href="#pid" class="headerlink" title="pid"></a>pid</h4><figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div></pre></div><div class="code"><pre><div class="line"><span class="attr">pid:</span> <span class="string">"host"</span></div></pre></div></div></figure>
<p>将PID模式设置为主机PID模式。 这将打开容器和主机操作系统之间的PID地址空间的共享。 使用此标志启动的容器将能够访问和操作裸机机器命名空间中的其他容器，反之亦然。</p>
<h4 id="ports"><a href="#ports" class="headerlink" title="ports"></a>ports</h4><p>公开端口。 指定两个端口（<code>HOST：CONTAINER</code>），或只指定容器端口（将选择随机主机端口）。</p>
<blockquote class="question"> <code>注意</code>：当映射<code>HOST:CONTAINER</code>格式的端口时，当使用低于60的容器端口时，您可能会遇到错误的结果，因为YAML将解析格式为<code>xx:yy</code>的数字为六进制（基数60）。 因此，我们建议您始终明确指定端口映射为字符串。<br></blockquote>

<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></div><div class="code"><pre><div class="line"><span class="attr">ports:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"3000"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"3000-3005"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"8000:8000"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"9090-9091:8080-8081"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"49100:22"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"127.0.0.1:8001:8001"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"127.0.0.1:5000-5010:5000-5010"</span></div></pre></div></div></figure>
<h4 id="security-opt"><a href="#security-opt" class="headerlink" title="security_opt"></a>security_opt</h4><p>覆盖每个容器的默认标签方案。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">security_opt:</span></div><div class="line"><span class="attr">    - label:</span><span class="attr">user:USER</span></div><div class="line"><span class="attr">    - label:</span><span class="attr">role:ROLE</span></div></pre></div></div></figure>
<h4 id="stop-signal"><a href="#stop-signal" class="headerlink" title="stop_signal"></a>stop_signal</h4><p>设置停止容器的备用信号。 默认情况下停止使用<code>SIGTERM</code>。 使用<code>stop_signal</code>设置备用信号将导致停止发送该信号。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div></pre></div><div class="code"><pre><div class="line"><span class="attr">stop_signal:</span> <span class="string">SIGUSR1</span></div></pre></div></div></figure>
<h4 id="ulimits"><a href="#ulimits" class="headerlink" title="ulimits"></a>ulimits</h4><p>覆盖容器的默认ulimits。 您可以将单个限制指定为整数或soft/hard限制作为映射。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></div><div class="code"><pre><div class="line"><span class="attr">ulimits:</span></div><div class="line"><span class="attr">    nproc:</span> <span class="number">65535</span></div><div class="line"><span class="attr">    nofile:</span></div><div class="line"><span class="attr">    soft:</span> <span class="number">20000</span></div><div class="line"><span class="attr">    hard:</span> <span class="number">40000</span></div></pre></div></div></figure>
<h4 id="volumes-volume-driver"><a href="#volumes-volume-driver" class="headerlink" title="volumes, volume_driver"></a>volumes, volume_driver</h4><p>装载路径或命名卷（可选）指定主机上的路径（<code>HOST:CONTAINER</code>）或访问模式（<code>HOST:CONTAINER:ro</code>）。 对于版本2文件，命名卷需要使用top-level volumes key`指定。 当使用版本1时，Docker Engine将自动创建命名卷（如果不存在）。</p>
<p>您可以在主机上安装相对路径，相对于正在使用的Compose配置文件的目录将相对路径。 相对路径应始终以<code>.</code>要么<code>..</code>。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></div><div class="code"><pre><div class="line"><span class="attr">volumes:</span></div><div class="line">    <span class="comment"># Just specify a path and let the Engine create a volume</span></div><div class="line"><span class="bullet">    -</span> <span class="string">/var/lib/mysql</span></div><div class="line"></div><div class="line">    <span class="comment"># Specify an absolute path mapping</span></div><div class="line"><span class="bullet">    -</span> <span class="string">/opt/data:/var/lib/mysql</span></div><div class="line"></div><div class="line">    <span class="comment"># Path on the host, relative to the Compose file</span></div><div class="line"><span class="bullet">    -</span> <span class="string">./cache:/tmp/cache</span></div><div class="line"></div><div class="line">    <span class="comment"># User-relative path</span></div><div class="line"><span class="bullet">    -</span> <span class="string">~/configs:/etc/configs/:ro</span></div><div class="line"></div><div class="line">    <span class="comment"># Named volume</span></div><div class="line"><span class="attr">    - datavolume:</span><span class="string">/var/lib/mysql</span></div></pre></div></div></figure>
<p>如果不使用主机路径，则可以指定<code>volume_driver</code>。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div></pre></div><div class="code"><pre><div class="line"><span class="attr">volume_driver:</span> <span class="string">mydriver</span></div></pre></div></div></figure>
<p>请注意，对于版本2文件，此驱动程序将不适用于named volumes（在使用<a href="https://docs.docker.com/compose/compose-file/#volume-configuration-reference" rel="external nofollow noopener noreferrer" target="_blank">declaring the volume</a>时，应使用<code>driver</code>选项）。 对于版本1，named volumes和container volumes都将使用指定的驱动程序。</p>
<blockquote class="question"> <code>注意</code>：如果您还指定了<code>volume_driver</code>，则不会执行路径扩展。<br></blockquote>

<p>有关更多信息，请参阅<a href="https://docs.docker.com/engine/userguide/dockervolumes/" rel="external nofollow noopener noreferrer" target="_blank">https://docs.docker.com/engine/userguide/dockervolumes/</a>和<a href="https://docs.docker.com/engine/extend/plugins_volume/" rel="external nofollow noopener noreferrer" target="_blank">Volume Plugins</a></p>
<h4 id="volumes-from"><a href="#volumes-from" class="headerlink" title="volumes_from"></a>volumes_from</h4><p>从另一个服务或容器装入所有卷，可选择指定只读访问（<code>ro</code>）或读写（<code>rw</code>）。 如果未指定访问级别，则将使用读写。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></div><div class="code"><pre><div class="line"><span class="attr">volumes_from:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">service_name</span></div><div class="line"><span class="attr">    - service_name:</span><span class="string">ro</span></div><div class="line"><span class="attr">    - container:</span><span class="string">container_name</span></div><div class="line"><span class="attr">    - container:</span><span class="attr">container_name:rw</span></div><div class="line"><span class="string">```</span> </div><div class="line"></div><div class="line"><span class="string">&lt;blockquote</span> <span class="string">class="question"&gt;</span> <span class="string">`注意`：`container:...`格式仅支持版本2文件格式。</span> <span class="string">在版本1中，您可以使用容器名称，而不将其标记为：</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="bullet">-</span> <span class="string">service_name</span></div><div class="line"><span class="attr">- service_name:</span><span class="string">ro</span></div><div class="line"><span class="bullet">-</span> <span class="string">container_name</span></div><div class="line"><span class="attr">- container_name:</span><span class="string">rw</span></div></pre></div></div></figure>
<p></p></blockquote><p></p>
<h4 id="cpu-shares-cpu-quota-cpuset-domainname-hostname-ipc-mac-address-mem-limit-memswap-limit-oom-score-adj-privileged-read-only-restart-shm-size-stdin-open-tty-user-working-dir"><a href="#cpu-shares-cpu-quota-cpuset-domainname-hostname-ipc-mac-address-mem-limit-memswap-limit-oom-score-adj-privileged-read-only-restart-shm-size-stdin-open-tty-user-working-dir" class="headerlink" title="cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc, mac_address, mem_limit, memswap_limit, oom_score_adj, privileged, read_only, restart, shm_size, stdin_open, tty, user, working_dir"></a>cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc, mac_address, mem_limit, memswap_limit, oom_score_adj, privileged, read_only, restart, shm_size, stdin_open, tty, user, working_dir</h4><p>每个都是一个单一的值，类似于它的docker运行对应。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></div><div class="code"><pre><div class="line"><span class="attr">cpu_shares:</span> <span class="number">73</span></div><div class="line"><span class="attr">cpu_quota:</span> <span class="number">50000</span></div><div class="line"><span class="attr">cpuset:</span> <span class="number">0</span><span class="string">,1</span></div><div class="line"></div><div class="line"><span class="attr">user:</span> <span class="string">postgresql</span></div><div class="line"><span class="attr">working_dir:</span> <span class="string">/code</span></div><div class="line"></div><div class="line"><span class="attr">domainname:</span> <span class="string">foo.com</span></div><div class="line"><span class="attr">hostname:</span> <span class="string">foo</span></div><div class="line"><span class="attr">ipc:</span> <span class="string">host</span></div><div class="line"><span class="attr">mac_address:</span> <span class="number">02</span><span class="string">:42:ac:11:65:43</span></div><div class="line"></div><div class="line"><span class="attr">mem_limit:</span> <span class="number">1000000000</span></div><div class="line"><span class="attr">memswap_limit:</span> <span class="number">2000000000</span></div><div class="line"><span class="attr">privileged:</span> <span class="literal">true</span></div><div class="line"></div><div class="line"><span class="attr">oom_score_adj:</span> <span class="number">500</span></div><div class="line"></div><div class="line"><span class="attr">restart:</span> <span class="string">always</span></div><div class="line"></div><div class="line"><span class="attr">read_only:</span> <span class="literal">true</span></div><div class="line"><span class="attr">shm_size:</span> <span class="number">64</span><span class="string">M</span></div><div class="line"><span class="attr">stdin_open:</span> <span class="literal">true</span></div><div class="line"><span class="attr">tty:</span> <span class="literal">true</span></div><div class="line"><span class="string">```</span> </div><div class="line"></div><div class="line"><span class="string">&lt;blockquote</span> <span class="string">class="question"&gt;`注意`：以下选项仅适用于版本2及更高版本：*</span> <span class="string">oom\_score\_adj</span></div><div class="line"><span class="string">&lt;/blockquote&gt;</span></div><div class="line"></div><div class="line"><span class="comment">### Volume configuration reference</span></div><div class="line"></div><div class="line"><span class="string">虽然可以作为服务声明的一部分来即时声明卷，但是此部分允许您创建可以跨多个服务重用的命名卷（不依赖`volumes_from`），并且可以使用docker命令行轻松检索和检查</span> <span class="string">或API。</span> <span class="string">有关更多信息，请参阅[docker</span> <span class="string">volume](https://docs.docker.com/engine/reference/commandline/volume_create/)子命令文档。</span></div><div class="line"></div><div class="line"><span class="comment">#### driver</span></div><div class="line"></div><div class="line"><span class="string">指定应为此卷使用哪个卷驱动程序。</span> <span class="string">默认为`local`。</span> <span class="string">如果驱动程序不可用，Docker</span> <span class="string">Engine将返回错误。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">driver:</span> <span class="string">foobar</span></div></pre></div></div></figure>
<h4 id="driver-opts"><a href="#driver-opts" class="headerlink" title="driver_opts"></a>driver_opts</h4><p>将选项列表指定为要传递给此卷的驱动程序的key-value pairs。 这些选项是驱动程序相关的 - 请参阅驱动程序文档以获取更多信息。 可选的。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">driver_opts:</span></div><div class="line"><span class="attr">    foo:</span> <span class="string">"bar"</span></div><div class="line"><span class="attr">    baz:</span> <span class="number">1</span></div></pre></div></div></figure>
<h4 id="external"><a href="#external" class="headerlink" title="external"></a>external</h4><p>如果设置为<code>true</code>，则指定该卷已在Compose外部创建。 <code>docker-compose up</code>不会尝试创建它，并且如果它不存在将会引发一个错误。</p>
<p><code>external</code>不能与其他卷配置键（<code>driver</code>，<code>driver_opts</code>）一起使用。</p>
<p>在下面的示例中，不是尝试创建名为<code>[projectname] _data</code>的卷，Compose将查找现有的卷，简单地称为数据，并将其挂载到db服务的容器中。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    db:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">postgres</span></div><div class="line"><span class="attr">    volumes:</span></div><div class="line"><span class="attr">        - data:</span><span class="string">/var/lib/postgresql/data</span></div><div class="line"></div><div class="line"><span class="attr">volumes:</span></div><div class="line"><span class="attr">    data:</span></div><div class="line"><span class="attr">    external:</span> <span class="literal">true</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="string">您还可以指定卷的名称与用于在Compose文件中引用它的名称：</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">volumes:</span></div><div class="line"><span class="attr">    data:</span></div><div class="line"><span class="attr">    external:</span></div><div class="line"><span class="attr">        name:</span> <span class="string">actual-name-of-volume</span></div></pre></div></div></figure>
<h4 id="labels-1"><a href="#labels-1" class="headerlink" title="labels"></a>labels</h4><blockquote class="question"> Added in version 2.1 file format.<br></blockquote>

<p>使用<a href="https://docs.docker.com/engine/userguide/labels-custom-metadata/" rel="external nofollow noopener noreferrer" target="_blank">Docker labels</a>向容器添加元数据。 您可以使用数组或字典。</p>
<p>建议您使用反向DNS符号来防止您的标签与其他软件使用的标签冲突。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></div><div class="code"><pre><div class="line"><span class="attr">labels:</span></div><div class="line">    <span class="string">com.example.description:</span> <span class="string">"Database volume"</span></div><div class="line">    <span class="string">com.example.department:</span> <span class="string">"IT/Ops"</span></div><div class="line">    <span class="string">com.example.label-with-empty-value:</span> <span class="string">""</span></div><div class="line"></div><div class="line"><span class="attr">labels:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.description=Database volume"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.department=IT/Ops"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.label-with-empty-value"</span></div></pre></div></div></figure>
<h3 id="Network-configuration-reference"><a href="#Network-configuration-reference" class="headerlink" title="Network configuration reference"></a>Network configuration reference</h3><p>顶层网络键允许您指定要创建的网络。 有关Compose使用Docker网络功能的完整说明，请参阅<a href="https://docs.docker.com/compose/networking/" rel="external nofollow noopener noreferrer" target="_blank">Networking guide</a>。</p>
<h4 id="driver"><a href="#driver" class="headerlink" title="driver"></a>driver</h4><p>指定应为此网络使用哪个驱动程序。</p>
<p>默认驱动程序取决于您使用的Docker引擎是如何配置的，但在大多数情况下，它将是单个主机上的<code>bridge</code>和Swarm上的<code>overlay</code>。</p>
<p>如果驱动程序不可用，Docker Engine将返回错误。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div></pre></div><div class="code"><pre><div class="line"><span class="attr">driver:</span> <span class="string">overlay</span></div></pre></div></div></figure>
<h4 id="driver-opts-1"><a href="#driver-opts-1" class="headerlink" title="driver_opts"></a>driver_opts</h4><p>指定选项列表作为要传递给此网络驱动程序的键值对。 这些选项是driver-dependent - consult的驱动程序文档的更多信息。 可选的。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></div><div class="code"><pre><div class="line"><span class="attr">driver_opts:</span></div><div class="line"><span class="attr">    foo:</span> <span class="string">"bar"</span></div><div class="line"><span class="attr">    baz:</span> <span class="number">1</span></div><div class="line"><span class="string">```</span>    </div><div class="line"></div><div class="line"><span class="comment">#### enable_ipv6</span></div><div class="line"></div><div class="line"><span class="string">&lt;blockquote</span> <span class="string">class="question"&gt;</span> <span class="string">Added</span> <span class="string">in</span> <span class="string">version</span> <span class="number">2.1</span> <span class="string">file</span> <span class="string">format.</span></div><div class="line"><span class="string">&lt;/blockquote&gt;</span></div><div class="line"></div><div class="line"><span class="string">在此网络上启用IPv6网络。</span></div><div class="line"></div><div class="line"><span class="comment">#### ipam</span></div><div class="line"></div><div class="line"><span class="string">指定自定义IPAM配置。</span> <span class="string">这是一个具有多个属性的对象，每个属性都是可选的：</span></div><div class="line"></div><div class="line"><span class="string">*</span>   <span class="string">driver：自定义IPAM驱动程序，而不是默认值。</span></div><div class="line"><span class="string">*</span>   <span class="string">config：具有零个或多个配置块的列表，每个配置块包含以下任意键：</span></div><div class="line">    <span class="string">*</span>   <span class="string">`subnet`:</span> <span class="string">表示网段的CIDR格式的子网</span></div><div class="line">    <span class="string">*</span>   <span class="string">`ip_range`:</span> <span class="string">从中分配容器IPs的IP范围</span></div><div class="line">    <span class="string">*</span>   <span class="string">`gateway`:</span> <span class="string">主子网的IPv4或IPv6网关</span></div><div class="line">    <span class="string">*</span>   <span class="string">`aux_addresses`：网络驱动程序使用的辅助IPv4或IPv6地址，作为从主机名到IP地址的映射</span></div><div class="line"><span class="string">*</span>   <span class="string">`options`:</span> <span class="string">作为键值映射的驱动特定选项。</span></div><div class="line"></div><div class="line"><span class="string">A</span> <span class="string">full</span> <span class="attr">example:</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">ipam:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">default</span></div><div class="line"><span class="attr">    config:</span></div><div class="line"><span class="attr">    - subnet:</span> <span class="number">172.28</span><span class="number">.0</span><span class="number">.0</span><span class="string">/16</span></div><div class="line"><span class="attr">        ip_range:</span> <span class="number">172.28</span><span class="number">.5</span><span class="number">.0</span><span class="string">/24</span></div><div class="line"><span class="attr">        gateway:</span> <span class="number">172.28</span><span class="number">.5</span><span class="number">.254</span></div><div class="line"><span class="attr">        aux_addresses:</span></div><div class="line"><span class="attr">        host1:</span> <span class="number">172.28</span><span class="number">.1</span><span class="number">.5</span></div><div class="line"><span class="attr">        host2:</span> <span class="number">172.28</span><span class="number">.1</span><span class="number">.6</span></div><div class="line"><span class="attr">        host3:</span> <span class="number">172.28</span><span class="number">.1</span><span class="number">.7</span></div><div class="line"><span class="attr">    options:</span></div><div class="line"><span class="attr">    foo:</span> <span class="string">bar</span></div><div class="line"><span class="attr">    baz:</span> <span class="string">"0"</span></div></pre></div></div></figure>
<h4 id="internal"><a href="#internal" class="headerlink" title="internal"></a>internal</h4><blockquote class="question"> Version 2 file format and up.<br></blockquote>

<p>默认情况下，Docker还将桥接网络连接到它以提供外部连接。 如果要创建外部隔离的覆盖网络，您可以将此选项设置为true。</p>
<h4 id="labels-2"><a href="#labels-2" class="headerlink" title="labels"></a>labels</h4><blockquote class="question"> Added in version 2.1 file format.<br></blockquote>

<p>使用Docker标签向容器添加元数据。 您可以使用数组或字典。</p>
<p>建议您使用反向DNS符号来防止您的标签与其他软件使用的标签冲突。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></div><div class="code"><pre><div class="line"><span class="attr">labels:</span></div><div class="line">    <span class="string">com.example.description:</span> <span class="string">"Financial transaction network"</span></div><div class="line">    <span class="string">com.example.department:</span> <span class="string">"Finance"</span></div><div class="line">    <span class="string">com.example.label-with-empty-value:</span> <span class="string">""</span></div><div class="line"></div><div class="line"><span class="attr">labels:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.description=Financial transaction network"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.department=Finance"</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"com.example.label-with-empty-value"</span></div><div class="line"><span class="string">```</span> </div><div class="line"></div><div class="line"><span class="comment">#### external</span></div><div class="line"></div><div class="line"><span class="string">如果设置为true，则指定此网络已在Compose之外创建。</span> <span class="string">`docker-compose</span> <span class="string">up`不会尝试创建它，并且如果它不存在将会引发一个错误。</span></div><div class="line"></div><div class="line"><span class="string">外部不能与其他网络配置键（`driver`，`driver_opts`，`group_add`，`ipam`，`internal`）一起使用。</span></div><div class="line"></div><div class="line"><span class="string">在下面的示例中，`proxy`是到外部世界的网关。</span> <span class="string">而不是尝试创建一个名为`[projectname]</span> <span class="string">_outside`的网络，Compose将查找一个现有的网络，简单地调用外`outside`并连接`outside`服务的容器。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    proxy:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">./proxy</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">outside</span></div><div class="line"><span class="bullet">        -</span> <span class="string">default</span></div><div class="line"><span class="attr">    app:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">./app</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">default</span></div><div class="line"></div><div class="line"><span class="attr">networks:</span></div><div class="line"><span class="attr">    outside:</span></div><div class="line"><span class="attr">    external:</span> <span class="literal">true</span></div></pre></div></div></figure>
<p>您还可以单独指定网络的名称，与用于在Compose文件中引用它的名称：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></div><div class="code"><pre><div class="line"><span class="attr">networks:</span></div><div class="line"><span class="attr">    outside:</span></div><div class="line"><span class="attr">    external:</span></div><div class="line"><span class="attr">        name:</span> <span class="string">actual-name-of-network</span></div></pre></div></div></figure>
<h3 id="Versioning"><a href="#Versioning" class="headerlink" title="Versioning"></a>Versioning</h3><p>目前有三个版本的Compose文件格式：</p>
<ul>
<li>版本1，传统格式。 这是通过省略YAML根目录下的版本键来指定的。</li>
<li>版本2，推荐格式。 这是使用版本：’2’条目在YAML的根目录指定。</li>
<li>版本2.1，升级超过版本2，利用了Docker Engine的最新特性。 使用版本：“2.1”条目指定YAML根目录。</li>
</ul>
<p>要将项目从版本1移动到2，请参阅升级部分。</p>
<blockquote class="question"> 注意：如果您使用多个Compose文件或扩展服务，每个文件必须是相同的版本 - 您不能在单个项目中混合版本1和2。<br></blockquote>

<p>根据您使用的版本，有几点不同：</p>
<ul>
<li>结构和允许的配置键</li>
<li>必须运行的最低Docker Engine版本</li>
<li>撰写关于网络的行为 这些差异如下所述。</li>
</ul>
<h4 id="Version-1"><a href="#Version-1" class="headerlink" title="Version 1"></a>Version 1</h4><p>未声明版本的组合文件被视为“版本1”。 在这些文件中，所有服务都在文档的根目录处声明。</p>
<p>版本1由Compose到1.6.x支持。 它将在未来的Compose版本中被弃用。</p>
<p>版本1文件无法声明命名卷，网络或构建参数。</p>
<p>例：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></div><div class="code"><pre><div class="line"><span class="attr">web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">.</span></div><div class="line"><span class="attr">    ports:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"5000:5000"</span></div><div class="line"><span class="attr">    volumes:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">.:/code</span></div><div class="line"><span class="attr">    links:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">redis</span></div><div class="line"><span class="attr">redis:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">redis</span></div></pre></div></div></figure>
<h4 id="Version-2"><a href="#Version-2" class="headerlink" title="Version 2"></a>Version 2</h4><p>使用版本2语法的撰写文件必须指示文档根目录下的版本号。 所有服务必须在服务键下声明。</p>
<p>Compose 1.6.0+支持版本2文件，并需要版本1.10.0+的Docker引擎。</p>
<p>可以在volumes键下声明命名卷，并且可以在networks关键字下声明网络。</p>
<p>简单示例：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">.</span></div><div class="line"><span class="attr">    ports:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">"5000:5000"</span></div><div class="line"><span class="attr">    volumes:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">.:/code</span></div><div class="line"><span class="attr">    redis:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">redis</span></div></pre></div></div></figure>
<p>一个更广泛的例子，定义卷和网络：</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></div><div class="code"><pre><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">    web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">.</span></div><div class="line"><span class="attr">    ports:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">"5000:5000"</span></div><div class="line"><span class="attr">    volumes:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">.:/code</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">front-tier</span></div><div class="line"><span class="bullet">        -</span> <span class="string">back-tier</span></div><div class="line"><span class="attr">    redis:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">redis</span></div><div class="line"><span class="attr">    volumes:</span></div><div class="line"><span class="attr">        - redis-data:</span><span class="string">/var/lib/redis</span></div><div class="line"><span class="attr">    networks:</span></div><div class="line"><span class="bullet">        -</span> <span class="string">back-tier</span></div><div class="line"><span class="attr">volumes:</span></div><div class="line"><span class="attr">    redis-data:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">local</span></div><div class="line"><span class="attr">networks:</span></div><div class="line"><span class="attr">    front-tier:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">bridge</span></div><div class="line"><span class="attr">    back-tier:</span></div><div class="line"><span class="attr">    driver:</span> <span class="string">bridge</span></div><div class="line"><span class="string">```</span>  </div><div class="line"></div><div class="line"><span class="comment">#### Version 2.1</span></div><div class="line"></div><div class="line"><span class="string">升级版本2，引入仅适用于Docker</span> <span class="string">Engine版本1.12.0+的新参数</span></div><div class="line"></div><div class="line"><span class="string">引入以下附加参数：</span> <span class="string">\*</span> <span class="string">link\_local\_ips</span> <span class="string">*</span> <span class="string">isolation</span> <span class="string">*</span> <span class="string">`labels`</span> <span class="string">for</span> <span class="string">volumes</span> <span class="string">and</span> <span class="string">networks</span></div><div class="line"></div><div class="line"><span class="comment">#### Upgrading</span></div><div class="line"></div><div class="line"><span class="string">在大多数情况下，从版本1移动到2是一个非常简单的过程：</span></div><div class="line"></div><div class="line"><span class="number">1.</span>  <span class="string">将整个文件缩进一级，并在顶部放置一个`services:`键。</span></div><div class="line"><span class="number">2.</span>  <span class="string">添加一个`version:</span> <span class="string">'2'</span><span class="string">`行在文件的顶部。</span></div><div class="line"></div><div class="line"><span class="string">如果您使用特定的配置功能，则更复杂：</span> <span class="string">\*</span> <span class="string">`dockerfile`:</span> <span class="string">This</span> <span class="string">now</span> <span class="string">lives</span> <span class="string">under</span> <span class="string">the</span> <span class="string">build</span> <span class="attr">key:</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">build:</span></div><div class="line"><span class="attr">context:</span> <span class="string">.</span></div><div class="line"><span class="attr">dockerfile:</span> <span class="string">Dockerfile-alternate</span></div></pre></div></div></figure>
<ul>
<li><code>log_driver</code>, <code>log_opt</code>: These now live under the <code>logging</code> key: <code>logging: driver: syslog options: syslog-address: &quot;tcp://192.168.0.42:123&quot;</code></li>
<li><p>与环境变量的链接：如环境变量引用中所述，由链接创建的环境变量已经过时了一段时间。 在新的Docker网络系统中，它们已被删除。 您应该直接连接到相应的主机名，或者自己设置相关的环境变量，使用link hostname： <figure class="highlight plain"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></div><div class="code"><pre><div class="line">*   db environment:</div><div class="line">*   DB_PORT=tcp://db:5432</div></pre></div></div></figure></p>
</li>
<li><p><code>external_links</code>：Compose在运行版本2项目时使用Docker网络，因此链接的行为略有不同。 特别地，两个容器必须共同连接到至少一个网络，以便进行通信，即使明确地链接在一起。 将外部容器连接到应用程序的默认网络，或者将外部容器和服务的容器都连接到外部网络。</p>
</li>
<li><code>net</code>: 这现在由network_mode替换:</li>
</ul>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></div><div class="code"><pre><div class="line"><span class="attr">net:</span> <span class="string">host</span>    <span class="bullet">-&gt;</span>  <span class="attr">network_mode:</span> <span class="string">host</span></div><div class="line"><span class="attr">net:</span> <span class="string">bridge</span>  <span class="bullet">-&gt;</span>  <span class="attr">network_mode:</span> <span class="string">bridge</span></div><div class="line"><span class="attr">net:</span> <span class="string">none</span>    <span class="bullet">-&gt;</span>  <span class="attr">network_mode:</span> <span class="string">none</span></div><div class="line"><span class="string">```</span>   </div><div class="line">    </div><div class="line"><span class="string">如果你使用</span> <span class="string">`net:</span> <span class="string">"container:[service name]"</span><span class="string">`,</span> <span class="string">用</span> <span class="string">`network_mode:</span> <span class="string">"service:[service name]"</span><span class="string">`</span> <span class="string">替换。</span></div><div class="line"></div><div class="line"><span class="string">```yml</span>    </div><div class="line"><span class="attr">net:</span> <span class="string">"container:web"</span>  <span class="bullet">-&gt;</span>  <span class="attr">network_mode:</span> <span class="string">"service:web"</span></div><div class="line"><span class="string">```</span>  </div><div class="line">    </div><div class="line"><span class="string">如果你使用</span> <span class="string">`net:</span> <span class="string">"container:[container name/id]"</span><span class="string">`,</span> <span class="string">值可以不变。</span></div><div class="line">    </div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">net:</span> <span class="string">"container:cont-name"</span>  <span class="bullet">-&gt;</span>  <span class="attr">network_mode:</span> <span class="string">"container:cont-name"</span></div><div class="line"><span class="attr">net:</span> <span class="string">"container:abc12345"</span>   <span class="bullet">-&gt;</span>  <span class="attr">network_mode:</span> <span class="string">"container:abc12345"</span></div><div class="line"><span class="string">```</span>  </div><div class="line">    </div><div class="line"><span class="string">*</span>   <span class="string">具有命名卷的卷：这些必须现在必须在Compose文件的顶级卷部分中显式声明。</span> <span class="string">如果服务装载称为数据的命名卷，则必须在顶层卷部分中声明数据卷。</span> <span class="string">整个文件可能如下所示：</span></div><div class="line"></div><div class="line"><span class="string">```yml</span>    </div><div class="line"><span class="attr">version:</span> <span class="string">'2'</span></div><div class="line"><span class="attr">services:</span></div><div class="line"><span class="attr">db:</span></div><div class="line"><span class="attr">    image:</span> <span class="string">postgres</span></div><div class="line"><span class="attr">    volumes:</span></div><div class="line"><span class="attr">    - data:</span><span class="string">/var/lib/postgresql/data</span></div><div class="line"><span class="attr">volumes:</span></div><div class="line"><span class="attr">data:</span> <span class="string">&#123;&#125;</span></div></pre></div></div></figure>
<p>默认情况下，Compose会创建一个以项目名称为前缀的卷。 如果你想要它只是被称为数据，声明它为外部</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></div><div class="code"><pre><div class="line"><span class="attr">volumes:</span></div><div class="line"><span class="attr">data:</span></div><div class="line"><span class="attr">external:</span> <span class="literal">true</span></div><div class="line"><span class="string">```</span>   </div><div class="line"></div><div class="line"><span class="comment">### Variable substitution</span></div><div class="line"></div><div class="line"><span class="string">您的配置选项可以包含环境变量。</span> <span class="string">Compose使用运行docker-compose的shell环境中的变量值。</span> <span class="string">例如，假设shell包含`EXTERNAL_PORT</span> <span class="string">=</span> <span class="number">8000</span><span class="string">`，并且您提供此配置：</span></div><div class="line"></div><div class="line"><span class="string">```yml</span></div><div class="line"><span class="attr">web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">.</span></div><div class="line"><span class="attr">    ports:</span></div><div class="line"><span class="bullet">    -</span> <span class="string">"$&#123;EXTERNAL_PORT&#125;:5000"</span></div></pre></div></div></figure>
<p>当使用此配置运行<code>docker-compose up</code>时，Compose会在shell中查找<code>EXTERNAL_PORT</code>环境变量并将其值代入。在本示例中，Compose在创建Web容器之前将端口映射解析为<code>“8000:5000”</code>。</p>
<p>如果未设置环境变量，则Compose将替换为空字符串。 在上面的示例中，如果未设置<code>EXTERNAL_PORT</code>，则端口映射的值为<code>:5000</code>（这当然是无效的端口映射，并且将在尝试创建容器时导致错误）。</p>
<p>支持<code>$VARIABLE</code>和<code>${VARIABLE}</code>语法。 此外，当使用2.1文件格式时，可以使用典型的shell语法提供内联默认值： <em> 如果<code>VARIABLE在</code>环境中未设置或为空，<code>${VARIABLE:-default}</code>将评估为默认值。 \</em> 只有在环境中未设置<code>VARIABLE</code>的情况下，<code>${VARIABLE-default}</code>才会评估为默认值。</p>
<p>不支持其他扩展的shell样式功能，例如<code>${VARIABLE/foo/bar}</code>。</p>
<p>当您的配置需要一个字母美元符号时，您可以使用<code>$$</code>（双美元符号）。 这也防止了Compose内插值，所以<code>$$</code>允许你引用你不想由Compose处理的环境变量。</p>
<figure class="highlight yml"><div class="figcode"><div class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></div><div class="code"><pre><div class="line"><span class="attr">web:</span></div><div class="line"><span class="attr">    build:</span> <span class="string">.</span></div><div class="line"><span class="attr">    command:</span> <span class="string">"$$VAR_NOT_INTERPOLATED_BY_COMPOSE"</span></div></pre></div></div></figure>
<p>如果您忘记并使用单个美元符号（<code>$</code>），Compose将该值解释为环境变量并将警告您：</p>
<p>未设置VAR_NOT_INTERPOLATED_BY_COMPOSE。 替换空字符串。</p>
<p>参考地址： [1]<a href="https://docs.docker.com/compose/compose-file" rel="external nofollow noopener noreferrer" target="_blank">https://docs.docker.com/compose/compose-file</a></p>
<hr>
      
    </div>

    
      


    

    
    
    
    
    <div>
        
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
        
    </div>
    
    <div>
          
            
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/how-to-write-dockercomposeyml.html">如何写docker-compose.yml</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 yszar 的个人博客">yszar</a></p>
  <p><span>发布时间:</span>2018年10月07日 - 22:10</p>
  <p><span>最后更新:</span>2018年10月29日 - 22:10</p>
  <p><span>原始链接:</span><a href="/how-to-write-dockercomposeyml.html" title="如何写docker-compose.yml">https://blog.codenote.pro/how-to-write-dockercomposeyml.html</a>
    <span class="copy-path" title="点击复制文章链接"><i class="fas fa-copy" data-clipboard-text="https://blog.codenote.pro/how-to-write-dockercomposeyml.html" aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="external nofollow noopener noreferrer" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>


          
    </div>

    
      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/uploads/wechat-qcode.jpg" alt="yszar wechat" style="width: 200px; max-width: 100%;">
    <div>欢迎您扫一扫上面的微信公众号，订阅我的博客！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/uploads/wxfk.png" alt="yszar 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/uploads/zfbfk.png" alt="yszar 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/docker/" rel="tag"><i class="fa fa-tag"></i> docker</a>
          
            <a href="/tags/docker-compose/" rel="tag"><i class="fa fa-tag"></i> docker-compose</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        
          <div class="wp_rating">
          <div style="color: rgba(0, 0, 0, 0.75); font-size:13px; letter-spacing:3px">(&gt;看完记得五星好评哦亲&lt;)</div>
            <div id="wpac-rating"></div>
          </div>
        

        

        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
            
              
              <a href="/code-block-add-copy-function.html" rel="prev" title="代码块添加复制功能">
                
                <i class="fa fa-chevron-left"></i> 代码块添加复制功能
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
            
              
              <a href="/docker-quickly-build-php-nginx-mysql-environment.html" rel="next" title="docker快速搭建php nginx mysql环境">
                
                docker快速搭建php nginx mysql环境 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
       <div id="gitalk-container"></div>
       <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">yszar</p>
              <p class="site-description motion-element" itemprop="description">学无止境！</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">21</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">42</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/yszar" target="_blank" title="GitHub" rel="external nofollow noopener noreferrer"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:yszar@vip.qq.com" target="_blank" title="E-Mail" rel="external nofollow noopener noreferrer"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank" rel="external nofollow noopener noreferrer">
                <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          

          
            
          
          
        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Service-配置参考"><span class="nav-number">1.</span> <span class="nav-text">Service 配置参考</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#build"><span class="nav-number">1.1.</span> <span class="nav-text">build</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#context"><span class="nav-number">1.1.1.</span> <span class="nav-text">context</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#dns"><span class="nav-number">1.2.</span> <span class="nav-text">dns</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#tmpfs"><span class="nav-number">1.3.</span> <span class="nav-text">tmpfs</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#env-file"><span class="nav-number">1.4.</span> <span class="nav-text">env_file</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#environment"><span class="nav-number">1.5.</span> <span class="nav-text">environment</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#expose"><span class="nav-number">1.6.</span> <span class="nav-text">expose</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#extends"><span class="nav-number">1.7.</span> <span class="nav-text">extends</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#external-links"><span class="nav-number">1.8.</span> <span class="nav-text">external_links</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#extra-hosts"><span class="nav-number">1.9.</span> <span class="nav-text">extra_hosts</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#group-add"><span class="nav-number">1.10.</span> <span class="nav-text">group_add</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#image"><span class="nav-number">1.11.</span> <span class="nav-text">image</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#isolation"><span class="nav-number">1.12.</span> <span class="nav-text">isolation</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#labels"><span class="nav-number">1.13.</span> <span class="nav-text">labels</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#logging"><span class="nav-number">1.14.</span> <span class="nav-text">logging</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#log-driver"><span class="nav-number">1.15.</span> <span class="nav-text">log_driver</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#log-opt"><span class="nav-number">1.16.</span> <span class="nav-text">log_opt</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#network-mode"><span class="nav-number">1.17.</span> <span class="nav-text">network_mode</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#networks"><span class="nav-number">1.18.</span> <span class="nav-text">networks</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#aliases"><span class="nav-number">1.18.1.</span> <span class="nav-text">aliases</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#ipv4-address-ipv6-address"><span class="nav-number">1.18.2.</span> <span class="nav-text">ipv4_address, ipv6_address</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#link-local-ips"><span class="nav-number">1.18.3.</span> <span class="nav-text">link_local_ips</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#pid"><span class="nav-number">1.19.</span> <span class="nav-text">pid</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ports"><span class="nav-number">1.20.</span> <span class="nav-text">ports</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#security-opt"><span class="nav-number">1.21.</span> <span class="nav-text">security_opt</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#stop-signal"><span class="nav-number">1.22.</span> <span class="nav-text">stop_signal</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ulimits"><span class="nav-number">1.23.</span> <span class="nav-text">ulimits</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#volumes-volume-driver"><span class="nav-number">1.24.</span> <span class="nav-text">volumes, volume_driver</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#volumes-from"><span class="nav-number">1.25.</span> <span class="nav-text">volumes_from</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#cpu-shares-cpu-quota-cpuset-domainname-hostname-ipc-mac-address-mem-limit-memswap-limit-oom-score-adj-privileged-read-only-restart-shm-size-stdin-open-tty-user-working-dir"><span class="nav-number">1.26.</span> <span class="nav-text">cpu_shares, cpu_quota, cpuset, domainname, hostname, ipc, mac_address, mem_limit, memswap_limit, oom_score_adj, privileged, read_only, restart, shm_size, stdin_open, tty, user, working_dir</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#driver-opts"><span class="nav-number">1.27.</span> <span class="nav-text">driver_opts</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#external"><span class="nav-number">1.28.</span> <span class="nav-text">external</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#labels-1"><span class="nav-number">1.29.</span> <span class="nav-text">labels</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Network-configuration-reference"><span class="nav-number">2.</span> <span class="nav-text">Network configuration reference</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#driver"><span class="nav-number">2.1.</span> <span class="nav-text">driver</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#driver-opts-1"><span class="nav-number">2.2.</span> <span class="nav-text">driver_opts</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#internal"><span class="nav-number">2.3.</span> <span class="nav-text">internal</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#labels-2"><span class="nav-number">2.4.</span> <span class="nav-text">labels</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Versioning"><span class="nav-number">3.</span> <span class="nav-text">Versioning</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Version-1"><span class="nav-number">3.1.</span> <span class="nav-text">Version 1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Version-2"><span class="nav-number">3.2.</span> <span class="nav-text">Version 2</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  
  <span class="with-love" id="heart">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yszar</span>

  

  
</div>


  










<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共38.6k字</span>
</div>
<div class="post-count">
	<a href="http://www.miitbeian.gov.cn/" rel="external nofollow noopener noreferrer" target="_blank">陇ICP备15003024号</a>
</div>


<div class="weixin-box">
  <div class="weixin-menu">
    <div class="weixin-hover">
      <div class="weixin-description">微信扫一扫，订阅本博客</div>
    </div>
  </div>
</div>
        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>












  















  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery@2.1.3/dist/jquery.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.4.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.4.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.2"></script>



  



  










   
     
       <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>

      <script src="/js/src/md5.min.js"></script>

       <script type="text/javascript">
         const gitalk = new Gitalk({
           clientID: '54f26977793794b45255',
           clientSecret: '568cccb3bdde42eca6c05ab0680b4617bbb58c4f',
           repo: 'yszar.github.io',
           owner: 'yszar',
           admin: 'yszar'.split(','),
           id: md5(location.pathname),
           pagerDirection: 'first',
           // facebook-like distraction free mode
           distractionFreeMode: false
         })
         gitalk.render('gitalk-container')
       </script>
     
   
 
  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script>
    
    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function ({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
              var $element = $(document.getElementById(url));
              $element.find('.leancloud-visitors-count').text(counter.time + 1);
            
            Counter('put', `/classes/Counter/${counter.objectId}`, JSON.stringify({ time: { "__op":"Increment", "amount":1 } }))
            
            .fail(function ({ responseJSON }) {
                console.log('Failed to save Visitor num, with error message: ' + responseJSON.error);
            })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1}))
                .done(function () {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function () {
                  console.log('Failed to create');
                });
            
          }
        })
      .fail(function ({ responseJSON }) {
        console.log('LeanCloud Counter Error:' + responseJSON.code + " " + responseJSON.error);
      });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + "iUS887d8BnkY3Gqc9FJX70fE-gzGzoHsz")
        .done(function ({ api_server }) {
          var Counter = function (method, url, data) {
            return $.ajax({
              method: method,
              url: `https://${api_server}/1.1${url}`,
              headers: {
                'X-LC-Id': "iUS887d8BnkY3Gqc9FJX70fE-gzGzoHsz",
                'X-LC-Key': "ekF4kKyQDgrDkIBadj3IDrVA",
                'Content-Type': 'application/json',
              },
              data: data,
            });
          };
          
          addCount(Counter);
          
        })
    });
  </script>



  

  

  

  
  
  <script type="text/javascript">
  wpac_init = window.wpac_init || [];
  wpac_init.push({widget: 'Rating', id: 14244,
    el: 'wpac-rating',
    color: 'fc6423'
  });
  (function() {
    if ('WIDGETPACK_LOADED' in window) return;
    WIDGETPACK_LOADED = true;
    var mc = document.createElement('script');
    mc.type = 'text/javascript';
    mc.async = true;
    mc.src = '//embed.widgetpack.com/widget.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
  })();
  </script>


  

  

  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('复制成功')
          else $(this).text('复制失败')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('复制')
        }, 300)
      }).append(e)
    })
  </script>


  
</body>
</html>
